package com.automannn.springPractice.zStudy;

import cn.hutool.core.io.IoUtil;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.FileUrlResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

/**
 * 通过代码了解资源的使用
 * @author weave
 * @since 2024-06-17 16:23
 */
public class ResourceStudyTest {
    public static void main(String[] args) throws IOException {
        javaResource();
        System.out.println("-------------------------------------");
        springResource();
        System.out.println("-------------------------------------");
        springResourceLoader();
    }

    /**
     * java中资源的使用方式
     * @throws IOException
     */
    private static void javaResource() throws IOException {
        URL url = new URL("file:///F:\\myself\\个人记录\\各种记录.txt");
        URLConnection connection = url.openConnection();
        InputStream inputStream = connection.getInputStream();
        String content = IoUtil.read(new InputStreamReader(inputStream, "gbk"));
        System.out.println(content);
    }

    /**
     * spring中资源的使用方式
     * @throws IOException
     */
    private static void springResource() throws IOException {
        Resource resource = new FileUrlResource("F:\\myself\\个人记录\\各种记录.txt");
        System.out.println(resource.exists());
        //获取资源输入流
        InputStream inputStream = resource.getInputStream();
        String content = IoUtil.read(new InputStreamReader(inputStream, "gbk"));
        System.out.println(content);
    }

    /**
     * spring中资源加载器的使用方式
     * @throws IOException
     */
    private static void springResourceLoader() throws IOException {
        //创建ResourceLoader
        ResourceLoader resourceLoader = new DefaultResourceLoader();
        //获取资源
        Resource resource = resourceLoader.getResource("http://www.baidu.com");
        //获取资源输入流
        InputStream inputStream = resource.getInputStream();
        String content = IoUtil.read(new InputStreamReader(inputStream));
        System.out.println(content);
    }
}
